package com.weilele.mvvm.widget.helper

import android.graphics.Typeface
import android.text.method.ScrollingMovementMethod
import android.util.AttributeSet
import android.widget.TextView
import com.weilele.mvvm.R
import com.weilele.mvvm.app
import com.weilele.mvvm.widget.BaseTextView
import java.io.File
import java.util.jar.Attributes


private val fontFaceMap by lazy { hashMapOf<String, Typeface>() }

/**
 * 设置assets里面的字体文件
 */
fun TextView.setFontFromAssets(assetsPath: String?) {
    assetsPath ?: return
    val cacheTypeFace = fontFaceMap[assetsPath]
    typeface = if (cacheTypeFace == null) {
        val newTypeFace = Typeface.createFromAsset(app.assets, assetsPath)
        fontFaceMap[assetsPath] = newTypeFace
        newTypeFace
    } else {
        cacheTypeFace
    }
}

/**
 * 从文件设置字体
 */
fun TextView.setFontFromFile(file: File?) {
    file ?: return
    val path = file.path
    val cacheTypeFace = fontFaceMap[path]
    typeface = if (cacheTypeFace == null) {
        val newTypeFace = Typeface.createFromFile(file)
        fontFaceMap[path] = newTypeFace
        newTypeFace
    } else {
        cacheTypeFace
    }
}

fun TextView.setFontFromFile(filePath: String?) {
    filePath ?: return
    setFontFromFile(File(filePath))
}
